Using the Graph API

Building an agent with LangGraph's Graph API involves several key steps:

  • Define tools and model
  • Define state structure
  • Define model node
  • Define tool node
  • Define conditional logic
  • Build and compile the agent

0Define Tools and Model

from langchain_core.tools import tool
from langchain.chat_models import init_chat_model

llm = init_chat_model(
    "anthropic:claude-3-7-sonnet-latest",
    temperature=0
)

@tool
def multiply(a: int, b: int) -> int:
    """Multiply a and b."""
    return a * b

@tool
def add(a: int, b: int) -> int:
    """Adds a and b."""
    return a + b

@tool
def divide(a: int, b: int) -> float:
    """Divide a and b."""
    return a / b

tools = [add, multiply, divide]
llm_with_tools = llm.bind_tools(tools)

1Define State

from langchain_core.messages import AnyMessage
from typing_extensions import TypedDict, Annotated
import operator

class MessagesState(TypedDict):
    messages: Annotated[list[AnyMessage], operator.add]
    llm_calls: int

The state defines the data structure that will be passed between nodes in the graph.

  • messages: List of conversation messages
  • llm_calls: Counter for tracking LLM invocations

2Define Model Node

from langchain_core.messages import SystemMessage

def llm_call(state: dict):
    """LLM decides whether to call a tool or not"""

    return {
        "messages": [
            llm_with_tools.invoke(
                [
                    SystemMessage(
                         content="You are a helpful assistant..."
                    )
                ]
                + state["messages"]
            )
        ],
        "llm_calls": state.get('llm_calls', 0) + 1
    }

3Define Tool Node

from langchain_core.messages import ToolMessage

def tool_node(state: dict):
    """Performs the tool call"""

    result = []
    for tool_call in state["messages"][-1].tool_calls:
        tool = tools_by_name[tool_call["name"]]
        observation = tool.invoke(tool_call["args"])
        result.append(ToolMessage(content=observation, tool_call_id=tool_call["id"]))
    return {"messages": result}

The tool node executes the tool calls requested by the LLM and returns the results.

4Define Conditional Logic

from typing import Literal
from langgraph.graph import StateGraph, START, END

def should_continue(state: MessagesState) -> Literal["tool_node", END]:
    """Decide if we should continue the loop or stop"""

    messages = state["messages"]
    last_message = messages[-1]
    if last_message.tool_calls:
        return "tool_node"
    return END

This function determines whether to route to the tool node or end the conversation based on whether the LLM made a tool call.

5Build And Invoke Agent

# Build workflow
agent_builder = StateGraph(MessagesState)

# Add nodes
agent_builder.add_node("llm_call", llm_call)
agent_builder.add_node("tool_node", tool_node)

# Add edges to connect nodes
agent_builder.add_edge(START, "llm_call")
agent_builder.add_conditional_edges(
    "llm_call",
    should_continue,
    ["tool_node", END]
)
agent_builder.add_edge("tool_node", "llm_call")

# Compile the agent
agent = agent_builder.compile()

# Invoke the agent
messages = agent.invoke({"messages":[HumanMessage(content="Add 3 and 4.")]})
for m in messages["messages"]:
    m.pretty_print()

Summary

The Graph API provides a structured way to build agents by:

  • Defining tools and models
  • Creating a state structure to track conversation data
  • Implementing nodes for LLM calls and tool execution
  • Adding conditional logic to control the flow
  • Connecting nodes with edges to form a complete workflow

This approach creates a flexible agent that can handle complex conversations with tool usage.